﻿using H2.NET.Business.Core;
using H2.NET.DBUtility;
using H2.NET.Web.Pages.SMD.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;

namespace H2.NET.Web.Pages.SMD.Service
{
    public class DealerDepositService
    {
        public DealerDepositService()
        {
            DbHelperSQL.connectionString = PubConstant.GetConnectionString("smdDB");
        }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_smd_Dealer");
            strSql.Append(" where code=@Code");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.Int,4)
			};
            parameters[0].Value = code;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(t_smd_DealerDeposit model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_smd_DealerDeposit(");
            strSql.Append("dealercode,region,name,amount)");
            strSql.Append(" values (");
            strSql.Append("@DealerCode,@Region,@Name,@Amount)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@DealerCode", SqlDbType.VarChar,50),
					new SqlParameter("@Region", SqlDbType.VarChar,50),
                    new SqlParameter("@Name", SqlDbType.VarChar,50),
                    new SqlParameter("@Amount", SqlDbType.Float,50)};
            parameters[0].Value = model.DealerCode;
            parameters[1].Value = model.Region;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Amount;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public bool DeleteAll()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_smd_DealerDeposit");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion  BasicMethod
    }
}